<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="tablecloth/tablecloth.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>

<?php

	
	include("inc/joomlaInt.php");
	include("inc/inc_tor.php");
	include("inc/inc_event.php");
	include("inc/inc_mail.php");
	include("inc/inc_configuration.php");
	

if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{		
	$tHi=$_POST["tHi"];
	$tGi=$_POST["tGi"];

	$matchId=$_POST["matchId"];
	
	$hG=$_POST["hG"];
	$gG=$_POST["gG"];
	$hOG=$_POST["hOG"];
	$gOG=$_POST["gOG"];
	$hY=$_POST["hY"];
	$gY=$_POST["gY"];
	$hYR=$_POST["hYR"];
	$gYR=$_POST["gYR"];
	$hR=$_POST["hR"];
	$gR=$_POST["gR"];

	
	// echo "hY:$hY";
	
	//TORE
	
			$count=1;
			for($count = 1; $count < $hG+1; $count++)
					{
						$ps="homeG$count";
						$tP=$_POST[$ps];
						$ms="homeGm$count";
						$tM=$_POST[$ms];
						$es="homeP$count";
						$penalty=$_POST["$es"];
						$type=1;
						if ($penalty==1){$type=5;}
						$anlegen=event_anlegen($type,$tP,$tM,$matchId,$tHi);
						// echo"$anlegen";
					}
			$count=1;
			for($count = 1; $count < $gG+1; $count++)
					{
						$ps="guestG$count";
						$tP=$_POST[$ps];
						$ms="guestGm$count";
						$tM=$_POST[$ms];
						$es="guestP$count";
						$penalty=$_POST["$es"];
						$type=1;
						if ($penalty==1){$type=5;}
						$anlegen=event_anlegen($type,$tP,$tM,$matchId,$tGi);
						// echo"$anlegen";
					}
					
	//EIGENTORE
	
			$count=1;
			for($count = 1; $count < $hOG+1; $count++)
					{
						$ps="homeOG$count";
						$tP=$_POST[$ps];
						$ms="homeOGm$count";
						$tM=$_POST[$ms];
						$type=6;
						$anlegen=event_anlegen($type,$tP,$tM,$matchId,$tHi);
						// echo"$anlegen";
					}
			$count=1;
			for($count = 1; $count < $gOG+1; $count++)
					{
						$ps="guestOG$count";
						$tP=$_POST[$ps];
						$ms="guestOGm$count";
						$tM=$_POST[$ms];
						$type=6;
						$anlegen=event_anlegen($type,$tP,$tM,$matchId,$tGi);
						// echo"$anlegen";
					}
	
	//Gelbe Karten
			
			$count=1;
			for($count = 1; $count < $hY+1; $count++)
					{
						$ps="homeY$count";
						$tP=$_POST[$ps];
						$ms="homeYm$count";
						$tM=$_POST[$ms];
						$anlegen=event_anlegen(2,$tP,$tM,$matchId,$tHi);
						// echo"$anlegen";
					}
			$count=1;
			for($count = 1; $count < $gY+1; $count++)
					{
						$ps="guestY$count";
						$tP=$_POST[$ps];
						$ms="guestYm$count";
						$tM=$_POST[$ms];
						$anlegen=event_anlegen(2,$tP,$tM,$matchId,$tGi);
						// echo"$anlegen";
					}
	
	//Gelb-Rote Karten
			
			$count=1;
			for($count = 1; $count < $hYR+1; $count++)
					{
						$ps="homeYR$count";
						$tP=$_POST[$ps];
						$ms="homeYRm$count";
						$tM=$_POST[$ms];
						$anlegen=event_anlegen(3,$tP,$tM,$matchId,$tHi);
						// echo"$anlegen";
					}
			$count=1;
			for($count = 1; $count < $gYR+1; $count++)
					{
						$ps="guestYR$count";
						$tP=$_POST[$ps];
						$ms="guestYRm$count";
						$tM=$_POST[$ms];
						$anlegen=event_anlegen(3,$tP,$tM,$matchId,$tGi);
						// echo"$anlegen";
					}
					
	
	//Rote Karten
			
			$count=1;
			for($count = 1; $count < $hR+1; $count++)
					{
						$ps="homeR$count";
						$tP=$_POST[$ps];
						$ms="homeRm$count";
						$tM=$_POST[$ms];
						$anlegen=event_anlegen(4,$tP,$tM,$matchId,$tHi);
						// echo"$anlegen";
					}
			$count=1;
			for($count = 1; $count < $gR+1; $count++)
					{
						$ps="guestR$count";
						$tP=$_POST[$ps];
						$ms="guestRm$count";
						$tM=$_POST[$ms];
						$anlegen=event_anlegen(4,$tP,$tM,$matchId,$tGi);
						// echo"$anlegen";
					}
	
	$cHiQ = "SELECT id, userId from $tbl_combinations WHERE teamId=$tHi AND active=1";
	$cGiQ = "SELECT id, userId from $tbl_combinations WHERE teamId=$tGi AND active=1";
	
	$cHiE = mysql_query($cHiQ);
	$cGiE = mysql_query($cGiQ);
	
	$cHi = mysql_fetch_object($cHiE);
	$cGi = mysql_fetch_object($cGiE);
	
	$cH = $cHi->id;
	$cG = $cGi->id;
	
	$uH = $cHi->userId;
	$uG = $cGi->userId;
	
	// echo "Home:$cH";
	
	$hCG=$hG+$gOG;
	$gCG=$gG+$hOG;
	
	$winner=9;
	if ($hCG>$gCG)
	{
		$winner=1;
	}
	else
	{
		if ($gCG>$hCG)
		{
			$winner=2;
		}
		else
		{
			$winner=3;
		}	
	}
	
	if ($jUser==$uH)
	{
		$hB=1;
		$gB=0;
	}
	else
		if ($jUser==$uG)
		{
			$hB=0;
			$gB=1;
		}
	
		
	$time = time();
	$query = "UPDATE $tbl_matches set homeG=$hCG, guestG=$gCG, homeC=$cH, guestC=$cG, free=2, playdate=$time, winner=$winner WHERE id =$matchId";
	$q = mysql_query($query);
	$query = "INSERT INTO $tbl_bestaetigung (matchId, homeB, guestB) 
				VALUES ($matchId, $hB, $gB)";
	$q = mysql_query($query);
	
	// echo"<br><br><br>$cH $cG";
	// echo"<br>$query";

	mailMatch($matchId);
	
	echo'Das Ergebnis wurde erfolgreich eingetragen.<br>Sie werden weitergeleitet... <meta http-equiv="refresh" content="2; URL=lima.php">';
}	
	
?>

</body>
</html>